﻿using AutoMapper;
using AutoMapper.Internal.Mappers;
using IntelligentAgriculture.Contracts.Dto.Input;
using IntelligentAgriculture.Contracts.Dto.Output;
using IntelligentAgriculture.Contracts.Interface;
using IntelligentAgriculture.Domain.Entity;
using IntelligentAgriculture.WebAPI.Application.Command.UserCommand;
using IntelligentAgriculture.WebAPI.Application.Command.VendorModelCommand;
using IntelligentAgriculture.WebAPI.Filter;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using MiniExcelLibs;

namespace IntelligentAgriculture.WebAPI.Controllers
{
	/// <summary>
	/// 供应商控制器
	/// </summary>
	[Route("api/[controller]/[action]")]
	[ApiController]
	public class VendorModelController : ControllerBase
	{
		private readonly IMediator _mediator;
		private readonly ILogger<VendorModelController> _logger;
		/// <summary>
		/// 构造方法
		/// </summary>
		/// <param name="mediator"></param>
		public VendorModelController(IMediator mediator, ILogger<VendorModelController> logger)
		{
			_mediator = mediator;
			_logger = logger;
		}
		/// <summary>
		/// 供应商列表
		/// </summary>
		/// <returns></returns>
		[HttpPost]
		public async Task<Results<PageOutput<Vendormodel>>> GetVendorModel(VendorModelInPut? inPut)
		{
			try
			{
				var command = new GetVendorModelCommand(inPut);
				return await _mediator.Send(command);
			}
			catch (Exception ex)
			{
				_logger.LogError("供应商列表异常" + ex.Message);
				throw;
			}
			
		}
		/// <summary>
		/// 删除
		/// </summary>
		/// <param name="command"></param>
		/// <returns></returns>
		[HttpPost]
		public async Task<Results<int>> DeleteVendormodels(params int[] ids)
		{
			try
			{
				var command = new DeleteVendormodelCommand(ids);
				return await _mediator.Send(command);
			}
			catch (Exception ex)
			{
				_logger.LogError("供应商删除异常" + ex.Message);
				throw;
			}
			
		}
		/// <summary>
		/// 数据导出
		/// </summary>
		/// <param name="searchCustomer"></param>
		/// <returns></returns>
		//[HttpGet]
		//public async Task< IActionResult> ExportCustomer([FromQuery] VendorModelInPut? inPut)
		//{
		//	var command = new GetVendorModelCommand(inPut);
		//	var list= await _mediator.Send(command);

		//	//获取数据
		//	var data = list.Data.Data;

		//	//转换为导出类型
		//	var exportCustomerDto = _mapper.Map<List<Vendormodel>, List<VendorModelminiExcelDTO>>(data);

		//	// 创建一个内存流对象
		//	MemoryStream ms = new MemoryStream();

		//	// 异步将导出的客户数据保存到内存流中
		//	await ms.SaveAsAsync(exportCustomerDto);

		//	// 将内存流的读取位置重置到开始位置
		//	ms.Seek(0, SeekOrigin.Begin);

		//	// 返回一个文件流结果，指定内容类型和文件下载名
		//	return new FileStreamResult(ms, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
		//	{
		//		FileDownloadName = "客户信息.xlsx"
		//	};
		//}
		/// <summary>
		/// 数据导出
		/// </summary>
		/// <param name="inPut"></param>
		/// <returns></returns>
		[HttpGet]
		public async Task<IActionResult> ExportCustomer([FromQuery] VendorModelInPut? inPut)
		{
			try
			{
				var command = new ExportCustomerCommand(inPut);
				return await _mediator.Send(command);
			}
			catch (Exception ex)
			{
				_logger.LogError("数据导出异常" + ex.Message);
				throw;
			}
			
		}
		///// <summary>
		///// 导入
		///// </summary>
		///// <param name="file"></param>
		///// <returns></returns>
		//[HttpPost]

		//public async Task<IActionResult> ImportCustomer(IFormFile file)
		//{
		//	try
		//	{
		//		// 读取文件内容
		//		MemoryStream memoryStream = new MemoryStream();
		//		//将传进来的文件拷贝到内存流中
		//		file.CopyTo(memoryStream);
		//		//通过内存流读取文件内容
		//		var list = await memoryStream.QueryAsync<VendorModelminiExcelInPut>();

		//		// 转换为实体
		//		var costomer = _mapper.Map<List<VendorModelminiExcelInPut>, List<Vendormodel>>(list.ToList());
		//		foreach (var item in costomer)
		//		{
		//			//批量添加
		//			await _vendorModelService.AddVendormodels(item);
		//		}
		//		return Ok();
		//	}
		//	catch (Exception)
		//	{
		//		throw;
		//	}
		//}
		/// <summary>
		/// 导入
		/// </summary>
		/// <param name="file"></param>
		/// <returns></returns>
		[HttpPost]

		public async Task<Results<bool>> ImportCustomer(IFormFile file)
		{
			try
			{
				var command = new ImportCustomerCommand(file);
				return await _mediator.Send(command);
			}
			catch (Exception ex)
			{
				_logger.LogError("导入异常" + ex.Message);
				throw;
			}
			
		}
		/// <summary>
		/// 添加
		/// </summary>
		/// <param name="entity">实体</param>
		/// <returns></returns>
		[HttpPost]
		
		public async Task<Results<int>> AddVendormodels(AddVendormodelsCommand command)
		{
			try
			{
				
				return await _mediator.Send(command);
			}
			catch (Exception ex)
			{
				_logger.LogError("添加异常" + ex.Message);
				throw;
			}
			
		}
		/// <summary>
		/// 供应商详情
		/// </summary>
		/// <param name="command"></param>
		/// <returns></returns>
		[HttpGet]

		public async Task<Results<Vendormodel>> DetailVendormodel(int vendorSnowId)
		{
			try
			{
				var command = new DetailVendormodelCommand(vendorSnowId);
				return await _mediator.Send(command);
			}
			catch (Exception ex)
			{
				_logger.LogError("供应商详情异常" + ex.Message);
				throw;
			}
			
		}
		/// <summary>
		/// 供应商修改
		/// </summary>
		/// <param name="command"></param>
		/// <returns></returns>
		[HttpPut]

		public async Task<Results<int>> UpdateVendormodels(UpdateVendormodelsCommand command)
		{
			try
			{
				return await _mediator.Send(command);
			}
			catch (Exception ex)
			{
				_logger.LogError("供应商详情异常" + ex.Message);
				throw;
			}

		}
	}
}
